package com.adb.androidwebalbum.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.adb.androidwebalbum.R;
import com.adb.androidwebalbum.bean.ImageBean;
import com.adb.androidwebalbum.utils.Log;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

/**
 * This fragment will populate the children of the ViewPager from {@link ImageDetailActivity}.
 */
public class ImageDetailFragment extends Fragment{
	
	public final String TAG = ImageDetailFragment.class.getCanonicalName();
	
	private static final String IMAGE_DATA_EXTRA = "resId";
	private int mImageNum;
	private ImageView mImageView;
	private ImageView temp_imageView;
	
	DisplayImageOptions mOptions;
	ImageDetailActivity mActivity;
	ArrayList<ImageBean> arrListImageBean;
	
    public static ImageDetailFragment newInstance(int imageNum) {
    	final ImageDetailFragment f = new ImageDetailFragment();
        final Bundle args = new Bundle();
        args.putInt(IMAGE_DATA_EXTRA, imageNum);
        f.setArguments(args);

        return f;
    }

    public ImageDetailFragment() {}
    
    @Override
    public void onAttach(Activity activity) {
    	super.onAttach(activity);
    	Log.debug(TAG, "[ onAttach ]");
    }
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.debug(TAG, "[ onCreate ]");
        mImageNum = getArguments() != null ? getArguments().getInt(IMAGE_DATA_EXTRA) : -1;
        
    }
    
  
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	Log.debug(TAG, "[ onCreateView ]");
        // Inflate and locate the main ImageView
        final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
        mImageView = (ImageView) v.findViewById(R.id.imageView);
        temp_imageView = (ImageView)v.findViewById(R.id.temp_imageView);
        
        if (ImageDetailActivity.class.isInstance(getActivity())) {
        	mActivity = (ImageDetailActivity)getActivity();
        	arrListImageBean = mActivity.getLatestArrayImageBean();
        	mOptions =  mActivity.getDisplayImageOptions();
        }
        
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.debug(TAG, "[ onActivityCreated ]");
        
        final ImageBean mImageBean = arrListImageBean.get(mImageNum);
        mActivity.imageLoader.displayImage(mImageBean.getThumbpath(), temp_imageView,mOptions);
        mActivity.imageLoader.displayImage(mImageBean.getLarge_filepath(), mImageView,mOptions,new ImageLoadingListener() {
			
			public void onLoadingStarted() {
				Log.println(TAG,"onLoadingStarted");
			}
			
			public void onLoadingFailed(FailReason arg0) {
				temp_imageView.setImageResource(android.R.drawable.ic_delete);
			}
			
			public void onLoadingComplete(Bitmap arg0) {
				 Log.println(TAG,"onLoadingComplete");
			}
			
			public void onLoadingCancelled() {
			}
		});
        
    
        // Pass clicks on the ImageView to the parent activity to handle
        if (OnClickListener.class.isInstance(getActivity())) {
        	 mImageView.setOnClickListener((OnClickListener) getActivity());
        	 
        }
    }
    
    @Override
    public void onStart() {
    	super.onStart();
    	Log.debug(TAG, "[ onStart ]");
    }
    @Override
    public void onResume() {
    	super.onResume();
    	Log.debug(TAG, "[ onResume ]");
    }
    @Override
    public void onStop() {
    	super.onStop();
    	Log.debug(TAG, "[ onStop ]");
    }
    @Override
    public void onDestroyView() {
    	super.onDestroyView();
    	Log.debug(TAG, "[ onDestroyView ]");
    	
    }
    @Override
    public void onPause() {
    	super.onPause();
    	Log.debug(TAG, "[ onPause ]");
    }
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	Log.debug(TAG, "[ onDestroy ]");
    }
    public void cancelWork(){
    	
    	if(mImageView !=null){
    	    mImageView.setImageDrawable(null);
    	    temp_imageView.setImageDrawable(null);
            mImageView = null;
            temp_imageView = null;
    	}
    }
    

}
